'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 来袭
 * @author sundbcn@gmail.com (sun debing)
 */

var _ = require('lodash');
var async = require('async');
var surviveConf = require('../config/surviveConf');
var surviveModel = require('../models/surviveModel');
var roleModel = require('../models/roleModel');
// var unionModel = require('../models/unionModel');
var unionGroupModel = require('../models/unionGroupModel');
var genRewardService = require('../services/genRewardService');
var roleService = require('../services/roleService');

exports.getRewardList = function (job_type, wave_num) {
    var reward_list = [];
    var reward_conf_list = surviveConf.getRewardListConf(job_type, wave_num);
    if (!!reward_conf_list) {
        _.each(reward_conf_list, function (reward_conf) {
            reward_list.push(genRewardService.generateReward(reward_conf));
        });
    }
    return reward_list;
};

exports.getSweepRewardList = function (job_type, wave_num) {
    var reward_list = [];
    var reward_conf_iterator = function (reward_conf) {
        reward_list.push(genRewardService.generateReward(reward_conf));
    };
    for (var i = 1; i <= wave_num; i++) {
        var reward_conf_list = surviveConf.getRewardListConf(job_type, i);
        if (!!reward_conf_list) {
            _.each(reward_conf_list, reward_conf_iterator);
        }
    }
    return reward_list;
};

/**
 * 当前玩家排名
 *
 */
exports.currentRoleRank = function (role_id, callback) {
    var current_role_rank = {};
    var survive_model;
    async.waterfall(
        [
            function (cb) {
                surviveModel.getByRoleId(role_id, cb);
            },
            function (model, cb) {
                survive_model = model;
                roleService.getRoleInfo(role_id, cb);
            },
            function (role_info, cb) {
                current_role_rank = role_info;
                current_role_rank.wave = survive_model.wave;
                current_role_rank.max_wave = survive_model.max_wave;
                surviveModel.getSurviveRankByRoleId(role_id, current_role_rank.max_wave, cb);
            },
            function (rank, cb) {
                current_role_rank.rank = rank;
                cb();
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return callback();
            }
            callback(null, current_role_rank);
        }
    );
};

/**
 * 生存排行版
 *
 */
exports.rank = function (cb) {
    surviveModel.getRank(function (err, role_list) {
        _getRoleInfoList(role_list, function (err, top_fifty_rank) {
            if (!!err) {
                console.error(err);
                return cb();
            }
            cb(null, top_fifty_rank);
        });
    });
};

var _getRoleInfoList = function (role_list, callback) {
    var top_fifty_rank = [];
    var rank = 1;
    var rank_role_info = {};
    async.eachSeries(role_list, function (role_item, cb) {
        surviveModel.getByRoleId(role_item.role_id, function (err, survive_model) {
            roleService.getRoleInfo(role_item.role_id, function (err, role_info) {
                if (!role_info) {
                    return cb();
                }
                rank_role_info = role_info;
                rank_role_info.rank = rank;
                rank_role_info.wave = survive_model.wave;
                rank_role_info.max_wave = survive_model.max_wave;
                top_fifty_rank.push(rank_role_info);
                rank++;
                cb();
            });
        });
    }, function (err) {
        if (!!err) {
            console.error(err);
            return callback();
        }
        callback(null, top_fifty_rank);
    });
};

var _getRoleInfo = function (role_item, callback) {
    var role_info = {};
    var role_model = {};
    async.series(
        [
            function (cb) {
                roleModel.getByRoleId(role_item.role_id, function (err, role_model) {
                    if (!!err) {
                        console.error(err);
                        return cb();
                    }
                    role_model = role_model;
                    if (role_model) {
                        role_info.role_id = role_model.role_id;
                        role_info.name = role_model.name;
                        role_info.job_type = role_model.job_type;
                        role_info.lv = role_model.lv;
                        role_info.vip = role_model.vip;
                        role_info.wave = role_item.max_wave;
                        role_info.max_wave = role_item.history_max_wave;
                    }
                    cb();
                });
            },
            function (cb) {
                if (!_.isEmpty(role_model)) {
                    // unionModel.getByRoleId(role_model.role_id, function (err, union_model) {
                    //     if (union_model.union_id !== 0) {
                    //         unionGroupModel.getById(union_model.union_id, function (err, union_group_model) {
                    //             role_info.union_name = union_group_model.name;
                    //         });
                    //     } else {
                    //         role_info.union_name = '';
                    //     }
                    //     cb();
                    // });
                } else {
                    role_info.union_name = '';
                    cb();
                }
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return callback();
            }
            callback(null, role_info);
        }
    );
};
